package com.miaoke;

import com.miaoke.entity.RpcTransferDto;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ClientSocket {
    public static Object callRemoteService(RpcTransferDto dto,String host,int port){
        ObjectInputStream ois=null;
        ObjectOutputStream oos=null;
        Object retObj = null;
        Socket socket = null;
        try {
            socket=new Socket(host,port);
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(dto);
            oos.flush();
            ois=new ObjectInputStream(socket.getInputStream());
            retObj = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (oos!=null)
                    oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(ois!=null){
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return retObj;
    }
}
